package co.fitstart.entity.user;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "user_source")
@DynamicUpdate
@JsonIgnoreProperties(value = { "hibernate_lazy_initializer", "handler", "fieldHandler" })
public class UserSource {

	@Id
	@GeneratedValue
	private Long id;

	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private User user;
	
	@Enumerated(EnumType.STRING)
    private UserSourceStatus userSourceStatus;

	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private UserSourceType userSourceType;
	
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private UserSourceType oldUserSourceType;

	private String userSourceTypeName;

	private String typeOther;

	private String name;

	private String gender;

	private String mobile;

	private String wechatnumber;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@org.hibernate.annotations.Type(type = "date")
	private Date createTime;

	/** 所属门店 线下订单 */
	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private Site site;

	/** 接待人 */
	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JSONField(serialize = false)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private Principal receiver;

	private String receiverName;

	private String requirement;

	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JSONField(serialize = false)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private Principal phoneReciver;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@org.hibernate.annotations.Type(type = "date")
	private Date occurDate;

	private String occurHour;

	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JSONField(serialize = false)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private Principal coach;

	/**
	 * 是否到店
	 */
	private Boolean visited;

	private String visitedReason;

	private Date visitedDate;

	/**
	 * 是否成单
	 */
	private Boolean ordered;

	private Date orderedDate;

	/**
	 * 教练评价
	 */
	private String coachComment;

	/**
	 * 接待人评价
	 */
	private String reciverCommment;

	/**
	 * 在营续课
	 */
	private Boolean continueOrder;

	/**
	 * 结营续课
	 */
	private Boolean stopOrder;

	@Column(name = "is_disabled")
	private boolean disabled;
	
	public boolean isNotFull() {
		
		if(this.receiver == null && StringUtils.isEmpty(this.receiverName)) {
			return true;
		}
		
		if(this.occurDate == null) {
			return true;
		}
		
		if(StringUtils.isEmpty(this.requirement)) {
			return true;
		}
		
		return false;
	}

	public UserSource() {
	}

	public Integer getTransIndex() {
		if ((this.userSourceType != null && this.userSourceType.getName().equalsIgnoreCase("大众点评"))
				|| (org.apache.commons.lang3.StringUtils.isNoneEmpty(this.userSourceTypeName)
						&& this.userSourceTypeName.equalsIgnoreCase("大众点评"))) {
			return 0;
		} else if ((this.userSourceType != null && this.userSourceType.getName().equalsIgnoreCase("访客"))
				|| (org.apache.commons.lang3.StringUtils.isNoneEmpty(this.userSourceTypeName)
						&& this.userSourceTypeName.equalsIgnoreCase("访客"))) {
			return 1;
		} else if ((this.userSourceType != null && this.userSourceType.getName().equalsIgnoreCase("转介绍"))
				|| (org.apache.commons.lang3.StringUtils.isNoneEmpty(this.userSourceTypeName)
						&& this.userSourceTypeName.equalsIgnoreCase("转介绍"))) {
			return 2;
		} else if ((this.userSourceType != null && this.userSourceType.getName().contains("店长"))
				|| (this.userSourceType != null && this.userSourceType.getParent() != null && this.userSourceType.getParent() == 9)
				|| (org.apache.commons.lang3.StringUtils.isNoneEmpty(this.userSourceTypeName)
						&& this.userSourceTypeName.contains("店长"))) {
			return 3;
		} else if ((this.userSourceType != null && this.userSourceType.getName().contains("线下"))
				|| (this.userSourceType != null && this.userSourceType.getParent() != null && this.userSourceType.getParent() == 7)
				|| (org.apache.commons.lang3.StringUtils.isNoneEmpty(this.userSourceTypeName)
						&& this.userSourceTypeName.contains("线下"))) {
			return 4;
		} else if (this.continueOrder != null && this.continueOrder == true) {
			return 5;
		} else if (this.stopOrder != null && this.stopOrder == true) {
			return 6;
		} else {
			return null;
		}
	}

	public UserSource(Date occurDate, String name, String mobile, String gender, String wechatnumber, String requirement, Site site,
			Principal receiver, Principal phoneReciver, UserSourceType userSourceType) {

		Date now = new Date();

		this.name = name;
		this.mobile = mobile;
		this.gender = gender;
		this.wechatnumber = wechatnumber;
		this.requirement = requirement;
		this.site = site;
		this.receiver = receiver;
		this.phoneReciver = phoneReciver;
		this.userSourceType = userSourceType;
		this.occurDate = occurDate;
		this.occurHour = DateFormatUtils.format(now, "HH:mm:ss");
		this.createTime = now;
	}

	public UserSource(String name, String mobile, String gender, String wechatNumber, String requirement,
			String reciverName, String dateSTring, String usersourceTypename) {
		this.name = name;
		this.mobile = mobile;
		this.gender = gender;
		this.wechatnumber = wechatNumber;
		this.requirement = requirement;
		this.receiverName = reciverName;
		this.userSourceTypeName = usersourceTypename;
		Date occurDate = DateUtils.parseDate(dateSTring, "MM/dd/yy");
		if(occurDate == null) {
			occurDate = DateUtils.parseDate(dateSTring, "yyyy/MM/dd");
		}
		this.occurDate = occurDate;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}
	
	/**
	 * @return the userSourceStatus
	 */
	public UserSourceStatus getUserSourceStatus() {
		return userSourceStatus;
	}

	/**
	 * @return the userSourceType
	 */
	public UserSourceType getUserSourceType() {
		return userSourceType;
	}
	
	/**
	 * @return the oldUserSourceType
	 */
	public UserSourceType getOldUserSourceType() {
		return oldUserSourceType;
	}

	/**
	 * @return the userSourceTypeName
	 */
	public String getUserSourceTypeName() {
		return userSourceTypeName;
	}

	/**
	 * @return the typeOther
	 */
	public String getTypeOther() {
		return typeOther;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the gender
	 */
	public String getGender() {
		return gender;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @return the wechatnumber
	 */
	public String getWechatnumber() {
		return wechatnumber;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the site
	 */
	public Site getSite() {
		return site;
	}

	/**
	 * @return the receiver
	 */
	public Principal getReceiver() {
		return receiver;
	}

	/**
	 * @return the receiverName
	 */
	public String getReceiverName() {
		return receiverName;
	}

	/**
	 * @return the requirement
	 */
	public String getRequirement() {
		return requirement;
	}

	/**
	 * @return the phoneReciver
	 */
	public Principal getPhoneReciver() {
		return phoneReciver;
	}

	/**
	 * @return the occurDate
	 */
	public Date getOccurDate() {
		return occurDate;
	}

	/**
	 * @return the occurHour
	 */
	public String getOccurHour() {
		return occurHour;
	}

	/**
	 * @return the coach
	 */
	public Principal getCoach() {
		return coach;
	}

	/**
	 * @return the visited
	 */
	public Boolean getVisited() {
		return visited;
	}

	/**
	 * @return the visitedReason
	 */
	public String getVisitedReason() {
		return visitedReason;
	}

	/**
	 * @return the visitedDate
	 */
	public Date getVisitedDate() {
		return visitedDate;
	}

	/**
	 * @return the ordered
	 */
	public Boolean getOrdered() {
		return ordered;
	}

	/**
	 * @return the orderedDate
	 */
	public Date getOrderedDate() {
		return orderedDate;
	}

	/**
	 * @return the coachComment
	 */
	public String getCoachComment() {
		return coachComment;
	}

	/**
	 * @return the reciverCommment
	 */
	public String getReciverCommment() {
		return reciverCommment;
	}

	/**
	 * @return the continueOrder
	 */
	public Boolean getContinueOrder() {
		return continueOrder;
	}

	/**
	 * @return the stopOrder
	 */
	public Boolean getStopOrder() {
		return stopOrder;
	}

	/**
	 * @return the disabled
	 */
	public boolean isDisabled() {
		return disabled;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * @param userSourceStatus the userSourceStatus to set
	 */
	public void setUserSourceStatus(UserSourceStatus userSourceStatus) {
		this.userSourceStatus = userSourceStatus;
	}

	/**
	 * @param userSourceType the userSourceType to set
	 */
	public void setUserSourceType(UserSourceType userSourceType) {
		this.userSourceType = userSourceType;
	}
	
	/**
	 * @param oldUserSourceType the oldUserSourceType to set
	 */
	public void setOldUserSourceType(UserSourceType oldUserSourceType) {
		this.oldUserSourceType = oldUserSourceType;
	}

	/**
	 * @param userSourceTypeName the userSourceTypeName to set
	 */
	public void setUserSourceTypeName(String userSourceTypeName) {
		this.userSourceTypeName = userSourceTypeName;
	}

	/**
	 * @param typeOther the typeOther to set
	 */
	public void setTypeOther(String typeOther) {
		this.typeOther = typeOther;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param gender the gender to set
	 */
	public void setGender(String gender) {
		this.gender = gender;
	}

	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @param wechatnumber the wechatnumber to set
	 */
	public void setWechatnumber(String wechatnumber) {
		this.wechatnumber = wechatnumber;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param site the site to set
	 */
	public void setSite(Site site) {
		this.site = site;
	}

	/**
	 * @param receiver the receiver to set
	 */
	public void setReceiver(Principal receiver) {
		this.receiver = receiver;
	}

	/**
	 * @param receiverName the receiverName to set
	 */
	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	/**
	 * @param requirement the requirement to set
	 */
	public void setRequirement(String requirement) {
		this.requirement = requirement;
	}

	/**
	 * @param phoneReciver the phoneReciver to set
	 */
	public void setPhoneReciver(Principal phoneReciver) {
		this.phoneReciver = phoneReciver;
	}

	/**
	 * @param occurDate the occurDate to set
	 */
	public void setOccurDate(Date occurDate) {
		this.occurDate = occurDate;
	}

	/**
	 * @param occurHour the occurHour to set
	 */
	public void setOccurHour(String occurHour) {
		this.occurHour = occurHour;
	}

	/**
	 * @param coach the coach to set
	 */
	public void setCoach(Principal coach) {
		this.coach = coach;
	}

	/**
	 * @param visited the visited to set
	 */
	public void setVisited(Boolean visited) {
		this.visited = visited;
	}

	/**
	 * @param visitedReason the visitedReason to set
	 */
	public void setVisitedReason(String visitedReason) {
		this.visitedReason = visitedReason;
	}

	/**
	 * @param visitedDate the visitedDate to set
	 */
	public void setVisitedDate(Date visitedDate) {
		this.visitedDate = visitedDate;
	}

	/**
	 * @param ordered the ordered to set
	 */
	public void setOrdered(Boolean ordered) {
		this.ordered = ordered;
	}

	/**
	 * @param orderedDate the orderedDate to set
	 */
	public void setOrderedDate(Date orderedDate) {
		this.orderedDate = orderedDate;
	}

	/**
	 * @param coachComment the coachComment to set
	 */
	public void setCoachComment(String coachComment) {
		this.coachComment = coachComment;
	}

	/**
	 * @param reciverCommment the reciverCommment to set
	 */
	public void setReciverCommment(String reciverCommment) {
		this.reciverCommment = reciverCommment;
	}

	/**
	 * @param continueOrder the continueOrder to set
	 */
	public void setContinueOrder(Boolean continueOrder) {
		this.continueOrder = continueOrder;
	}

	/**
	 * @param stopOrder the stopOrder to set
	 */
	public void setStopOrder(Boolean stopOrder) {
		this.stopOrder = stopOrder;
	}

	/**
	 * @param disabled the disabled to set
	 */
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
}
